package com.jcfk.eam.service.base;

import com.jcfk.eam.domain.po.base.EamMaker;
import com.jcfk.eam.domain.query.base.EamMakerQuery;
import com.jcfk.eam.domain.dto.base.EamMakerDTO;
import com.jcfk.eam.domain.dto.base.EamMakerContactDTO;
import com.jcfk.eam.domain.po.base.EamMakerContact;
import com.jcfk.eam.dao.base.EamMakerDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.List;

import java.util.Date;

/**
 * 制造商
 *
 * @author yql
 * @email 522891237@gqq.com
 * @date 2024-07-24
 */
@Service
public class EamMakerService extends BaseService<EamMakerDao,EamMaker> {


    @Autowired
    private EamMakerContactService eamMakerContactService;

    /**
     * EamMaker分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamMakerDTO> queryByPage(DataPage<EamMakerDTO> page, EamMakerQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamMakerDTO>
     */
    public List<EamMakerDTO> getList(EamMakerQuery query) {
        // 查询条件
        QueryWrapper<EamMaker> qw = new QueryWrapper<>();
        qw.lambda().eq(query.getAssetKind() != null, EamMaker::getAssetKind, query.getAssetKind());
        qw.lambda().eq(!StrUtil.isEmpty(query.getMakerCode()), EamMaker::getMakerCode, query.getMakerCode());
        qw.lambda().eq(!StrUtil.isEmpty(query.getMakerName()), EamMaker::getMakerName, query.getMakerName());
        qw.lambda().eq(query.getEnabled() != null, EamMaker::getEnabled, query.getEnabled());
        qw.lambda().eq(query.getDeleted() != null, EamMaker::getDeleted, query.getDeleted());
        // 查询数据
        List<EamMaker> list = super.list(qw);

        return convertList(list, EamMakerDTO.class);
    }

    /**
     * 获取根据关联id详情表列表
     *
     * @param query 查询条件
     * @return List<EamMakerDTO>
     */
    public List<EamMakerContactDTO> getDetails(EamMakerQuery query) {
        // 查询条件
        QueryWrapper<EamMakerContact> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getId()), EamMakerContact::getMakerId, query.getId());
        qw.lambda().eq(query.getDeleted() != null, EamMakerContact::getDeleted, query.getDeleted());

        // 查询数据
        List<EamMakerContact> list = eamMakerContactService.list(qw);

        return convertList(list, EamMakerContactDTO.class);
    }


    /**
     * 判断EamMaker是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamMakerDTO dto) {
        QueryWrapper<EamMaker> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        return RetResult.ok();
    }

    /**
     * 插入制造商
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamMakerDTO dto) {

        // 保存
        EamMaker item = new EamMaker();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);

        // 设置关联主键
        for (EamMakerContactDTO detail : dto.getDetails()) {
            detail.setMakerId(item.getId());
        }

        // 保存明细表
        eamMakerContactService.saveBatch(dto.getDetails(), item.getId());
    }


    /**
     * 更新制造商
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamMakerDTO dto) {

        // 保存
        EamMaker item = new EamMaker();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);

        // 设置关联主键
        for (EamMakerContactDTO detail : dto.getDetails()) {
            detail.setMakerId(item.getId());
        }
        // 保存明细表
        eamMakerContactService.saveBatch(dto.getDetails(), item.getId());
    }


    /**
     * 根据批量主键删除制造商(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamMakerDTO dto) {

        EamMaker item = new EamMaker();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamMaker> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamMaker item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());
    }


    /**
     * 根据主键获取制造商信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamMakerDTO get(String id) {
        // 查询数据
        EamMaker item = this.baseMapper.selectById(id);
        EamMakerDTO dto = new EamMakerDTO();
        try {
            BeanUtils.copyProperties(item, dto);
            EamMakerQuery query = new EamMakerQuery();
            query.setId(item.getId());
            query.setDeleted(0);
            dto.setDetails(getDetails(query));
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据主键获取制造商信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteitem(String id) {
        EamMaker item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

